import requests

# 执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}") # 状态代码：200代表成功
# 将API响应赋给一个变量
response_dict = r.json() # r.json()把传回的json数据转化成字典返回

# 处理结果
# print(response_dict.keys()) # 查看返回字典的键

print(f"Total repositories: {response_dict['total_count']}")

# 探索有关仓库的信息
repo_dicts = response_dict['items'] # 'items'键对应的值是一个列表，包含所有数据
print(f"Repositories returned: {len(repo_dicts)}") # 输出列表数据的数量

# 研究第一个仓库
# repo_dict = repo_dicts[0]
# print(f"\nKeys: {len(repo_dict)}")
# for key in sorted(repo_dict.keys()): # 打印一个仓库数据中所有键
#     print(key)

print("\nSelected information about each repository:")
for repo_dict in repo_dicts:
    print(f"Name: {repo_dict['name']}")
    print(f"Owner: {repo_dict['owner']['login']}")
    print(f"Stars: {repo_dict['stargazers_count']}")
    print(f"Repository: {repo_dict['html_url']}")
    # print(f"Created: {repo_dict['created_at']}")
    # print(f"Updated: {repo_dict['updated_at']}")
    print(f"Discription: {repo_dict['description']}")
    print()